
#ifndef __KEY_TABLE_VIEW_H
#define __KEY_TABLE_VIEW_H

#include "../crypt/Key.h"

class NewKeyDB;

/** An interface to interact with a single table of keys with the consistent
 * uses. */
class KeyTableView
{
   public:
      enum keytableusetype {   KEY_TABLE_USE_TYPE__ENCRYPTION = 0,
                               KEY_TABLE_USE_TYPE__DECRYPTION = 1,
                               KEY_TABLE_USE_TYPE__SIGNING    = 2,
                               KEY_TABLE_USE_TYPE__CHECKING   = 3   };
      enum keyopenerror    {   KEY_OPEN_ERROR__NO_ERROR          = 0,
                               KEY_OPEN_ERROR__INVALID_KEY_NAME  = 1,
                               KEY_OPEN_ERROR__INVALID_KEY_DST   = 2,
                               KEY_OPEN_ERROR__KEY_NOT_FOUND     = 3,
                               KEY_OPEN_ERROR__FAILED_SANITY     = 4,
                               KEY_OPEN_ERROR__KEY_IMPORT_FAILED = 5,
                               KEY_OPEN_ERROR__KEY_NULL          = 6   };
      enum keyinserterror  {   KEY_INSERT_ERROR__NO_ERROR         = 0,
                               KEY_INSERT_ERROR__INVALID_KEY_NAME = 1,
                               KEY_INSERT_ERROR__INVALID_KEY_SRC  = 2,
                               KEY_INSERT_ERROR__EXPORT_ERROR     = 3   };
      KeyTableView(NewKeyDB *newkdb, int ikeytableusetype,
                   const char *itablename, const char *itableWIPpwd,
                   const char *iresidentkeyname, const char *iresidentkeypwd);
      ~KeyTableView();
      int countKeyChainOfOwner(const char *owner);
      char *getLatestKeyNameOfOwner(const char *owner);
      char getMarkOfKey(const char *keyname);
      void setMarkOfKey_unsafe(const char *keyname, char newmark);
      void setKeyToExpire_unsafe(const char *keyname);
      void retryGettingOwnKeys(const char *ownpwd);
      int tryToInsertKeyIntoTable(const char *name, const char *pwd, char sig, const char *owner, Key *key);
      int tryToInsertKeyIntoTable_tsafe(const char *name, const char *pwd, char sig, const char *owner, Key *key, int uselock);
      void tryToInsertKeyErrorCheck(const char *keyname, int terrno);
      int tryToGetKeyFromTable(const char *name, const char *pwd, Key **key);
      void tryToGetKeyErrorCheck(const char *keyname, int terrno);
      int isGeneratingNewOwnKeysAcceptable() { return couldgeneratekeysflag; }
   protected:
      /** Pointer to the key database, needed to interact with the actual
       * SQL database. */
      NewKeyDB *nkeydb;
      /** A flag denoting the use scenario for keys in this table.
       * One of KeyTableView::keytableusetype. */
      int keytableusetype;
      /** The name of the table that this view interfaces with. */
      char *tablename;
      /** The work-in-progress password used for this table. */
      char *tableWIPpwd;
      /** The name of the resident key that defines the identity of the main
       * actor. */
      char *residentkeyname;
      /** The key from this table that defines the identity of the main
       * actor. */
      Key *ownkey;
      /** Whether it is permissible to generate new keys on a failure.
       * This is set when there are no keys, and unset when there are keys
       * present.
       * Checking this before regenerating keys prevents overwriting of keys
       * by unauthorised users. */
      int couldgeneratekeysflag;
};

// Could add EncryptionKeyTable, DecryptionKeyTable etc...

#endif
